﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace UI {
    public class OpenSverItem : MonoBehaviour
    {
        public GameObject icon;
        public UILabel levelLimit;
        public UILabel getName;
        public GameObject commonIcon;
        public GameObject unReceive;
        public GameObject btnGet;
        public GameObject imgGetted;
        public delegate void AddRedNum();
        public AddRedNum m_addNum;
        const string openSever = "openServerFund";
        const string allPerson = "everyoneBenefit";
        int tabIndex;
        //int oneOrAll;
        //string activityId;
        int itemIndex;
        UIActivityOpenSeverMoney script;
        public void SetActivityItem(ActivityItem activityItem, string acId,UIActivityOpenSeverMoney script,int flag)
        {
            //activityId = acId;
            itemIndex = activityItem.index;
            this.script = script;
            tabIndex = flag;
            //奖励
            commonIcon.GetComponent<CommonIcon>().SetData(activityItem.rewardList[0].id, true);
            getName.text = activityItem.rewardList[0].num.ToString() + TableTextBlo.GetText(CommonMethod.GetObjectNameTextId(activityItem.rewardList[0].id));

            if (flag == 0)
                levelLimit.text = string.Format(TableTextBlo.GetText("activity_openServer_getLevel_Limit"), activityItem.index);//开服基金
            else
                levelLimit.text = string.Format(TableTextBlo.GetText("activity_allPerson_NumLimit"), activityItem.index);//全民福利
            
            //oneOrAll = activityItem.pickOneOrGetAll;

            //领奖状态
            unReceive.SetActive(activityItem.status == 0);
            btnGet.SetActive(activityItem.status == 1);
            imgGetted.SetActive(activityItem.status == 2);
            if (activityItem.status == 1 && m_addNum != null)
                m_addNum();
        }

        void  GetTextString()
        {
            TableTextBlo.GetText("activity_allPerson_NumLimit");
        }

        public void OnBtnGet()
        {
            CSOpenServerFundReward msg = new CSOpenServerFundReward();
            if (tabIndex == 0)
                msg.rewardType = openSever;
            else
                msg.rewardType = allPerson;
            msg.index = itemIndex;
            NetAPI.SendHttp(OpDefine.CSOpenServerFundReward, msg, GetRewardCallback);
        }

        void GetRewardCallback(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCOpenServerFundReward msg = packet.kBody as SCOpenServerFundReward;
                StaticData.playerData.AddResultObj(msg.get);
                CommonMethod.ShowGetObjs(msg.get);
                unReceive.SetActive(false);
                btnGet.SetActive(false);
                imgGetted.SetActive(true);
                if (tabIndex == 0)
                    script.OnOpenMoney(msg.refreshList);
                else
                    script.OnAllPerson(msg.refreshList);
                //script.ReduceCanGetNum();
            }
        }
    }
}

